﻿using System;
using System.Collections.Generic;
using AChance.Bll.Func;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace FutureSerWeb.Func
{
    public static class AppService
    {
        /// <summary>
        /// 状态
        /// </summary>
        public static class State
        {
            /// <summary>
            /// 执行成功
            /// </summary>
            public const string SUCCESS = "0000";
            /// <summary>
            /// 无效会话
            /// </summary>
            public const string INVALID_SESSION = "1000";
            /// <summary>
            /// 频繁请求
            /// </summary>
            public const string FREQUENT_REQ = "1001";
            /// <summary>
            /// 执行失败
            /// </summary>
            public const string FAIL = "9999";
            /// <summary>
            /// 获取描述
            /// </summary>
            public static string GetDesc(string state) =>
                state switch
                {
                    SUCCESS => "执行成功",
                    INVALID_SESSION => "无效会话",
                    FREQUENT_REQ => "频繁请求",
                    _ => "执行失败"
                };
        }

        /// <summary>
        /// 请求包
        /// </summary>
        [Serializable]
        public class ReqPkg : BllTrans.TransPkg
        {
            protected string _inToken = "";
            protected string _inSession = "";
            protected string _inAppName = "";
            protected string _inAppType = "";
            protected string _inAppVer = "";
            protected string _inAppCode = "";
            protected string _inOrganNo = "";

            /// <summary>
            /// 传入令牌
            /// </summary>
            public string InToken
            {
                get { return _inToken; }
                set { _inToken = value?.Trim() ?? ""; }
            }
            /// <summary>
            /// 传入会话
            /// </summary>
            public string InSession
            {
                get { return _inSession; }
                set { _inSession = value?.Trim() ?? ""; }
            }
            /// <summary>
            /// 传入App名称
            /// </summary>
            public string InAppName
            {
                get { return _inAppName; }
                set { _inAppName = value?.Trim() ?? ""; }
            }
            /// <summary>
            /// 传入App类型
            /// </summary>
            public string InAppType
            {
                get { return _inAppType; }
                set { _inAppType = value?.Trim() ?? ""; }
            }
            /// <summary>
            /// 传入App版本
            /// </summary>
            public string InAppVer
            {
                get { return _inAppVer; }
                set { _inAppVer = value?.Trim() ?? ""; }
            }
            /// <summary>
            /// 传入App编码
            /// </summary>
            public string InAppCode
            {
                get { return _inAppCode; }
                set { _inAppCode = value?.Trim() ?? ""; }
            }
            /// <summary>
            /// 传入机构编号
            /// </summary>
            public string InOrganNo
            {
                get { return _inOrganNo; }
                set { _inOrganNo = value?.Trim() ?? ""; }
            }

            #region 应答内容处理
            /// <summary>
            /// 获取应答内容
            /// </summary>
            public object GetRespContent()
            {
                return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(this, responseContentJsonSerializerSettings));
            }
            private static JsonSerializerSettings responseContentJsonSerializerSettings = new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore,
                ContractResolver = new RespContentContractResolver()
            };
            private class RespContentContractResolver : DefaultContractResolver
            {
                private static List<string> includeProperty = new List<string>
                {
                    nameof(OutState),
                    nameof(OutMsg),
                    nameof(OutMsgDetail),
                    nameof(OutData)
                };
                protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
                {
                    var properties = (List<JsonProperty>)base.CreateProperties(type, memberSerialization);
                    if (type.Name.Equals(nameof(ReqPkg)))
                    {
                        properties = properties.FindAll(property => includeProperty.Contains(property.PropertyName));
                        properties.ForEach(property => property.PropertyName = property.PropertyName.Substring(0, 1).ToLower() + property.PropertyName.Substring(1));
                        return properties;
                    }
                    else
                        return properties;
                }
            }
            #endregion

        }
    }
}